Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@jalik/observer

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@jalik/observer

A library to observe events and trigger callbacks.

  • 1.2.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
87
increased by11.54%
Maintainers
1
Weekly downloads
 
Created
Source

@jalik/observer

GitHub package.json version Build Status GitHub GitHub last commit GitHub issues npm

Observe and emit events with ease.

Includes TypeScript declaration files.

Introduction

The Observer design pattern is a well known pattern to create reactive applications. For example, your can attach observers to a form text field, then when the text field value changes, all observers are notified of that change and thus can do something in response.

Attaching an observer and notify it

The following code shows how to attach an observer and how to notify it of events.

import Observer from "@jalik/observer";

class Person {
  constructor(name) {
    this.name = name;
    // Create the observer with current context
    this.observer = new Observer(this);
  }

  on(event, observer) {
    // Attach observer
    this.observer.attach(event, observer);
  }

  say(words) {
    // Notify observers
    this.observer.notify("say", words, new Date());
  }
}

const karl = new Person("karl");

karl.on("say", function (words, date) {
  console.log(`${this.name} said: "${words}"`);
});
// this will show: karl said: "hello"
karl.say("hello");

Detaching an observer

In the case that you need to remove a previously attached observer, here is the code.

import Observer from "@jalik/observer";

const doubleClickListener = function () {
  console.log("double click detected");
  // This avoid the current function to be called
  // on the next "doubleClick" event notification.
  observer.detach("doubleClick", doubleClickListener);
};

const observer = new Observer();
observer.attach("doubleClick", doubleClickListener);

// This will call the doubleClickListener once.
observer.notify("doubleClick");

// This will not call the doubleClickListener
// since it has been detached in the previous call.
observer.notify("doubleClick");

Changelog

History of releases is in the changelog.

License

The code is released under the MIT License.

If you find this lib useful and would like to support my work, donations are welcome :)

Donate

Keywords

FAQs

Package last updated on 15 Apr 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc